<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">表号</label>
        <el-input v-model="query.meterNum" clearable placeholder="请输入表号搜索" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否注册</label>
        <el-select
          v-model="query.registered"
          clearable
          size="small"
          placeholder="是否注册"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in registeredTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <label class="el-form-item-label">开/合闸</label>
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="开/合闸状态"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in statusTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <label class="el-form-item-label">创建时间</label>
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <label class="el-form-item-label">更新时间</label>
        <date-range-picker
          v-model="query.updateTime"
          start-placeholder="开始时间"
          end-placeholder="创建时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="85px">
          <el-form-item label="表号" prop="meterNum">
            <el-input v-model="form.meterNum" style="width: 360px;" />
          </el-form-item>
          <el-form-item label="电流">
            <el-input v-model="form.currentC" disabled style="width: 360px;" />
          </el-form-item>
          <el-form-item label="电压">
            <el-input v-model="form.voltageC" disabled style="width: 360px;" />
          </el-form-item>
          <el-form-item label="有功总电量">
            <el-input v-model="form.totalActivePower" disabled style="width: 360px;" />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" disabled  type="datetime" style="width: 360px;" />
          </el-form-item>
          <el-form-item label="是否注册">
            <el-radio-group v-model="form.registered" style="width: 178px">
              <el-radio :label="true">已注册</el-radio>
              <el-radio :label="false">未注册</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="开/合闸">
            <el-radio-group v-model="form.status" disabled  style="width: 178px">
              <el-radio :label="true">开闸</el-radio>
              <el-radio :label="false">合闸</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="form.updateTime" disabled  type="datetime" style="width: 360px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" stripe v-loading="crud.loading" :data="crud.data"
        size="small" style="width: 100%;margin-top: 1%;"
        :header-cell-style="{background: '#fff5ee',color:'#80878f'}"
      @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="meterNum" sortable label="表号" />
        <el-table-column prop="cabinet.name" label="所属充电桩名称" />
        <el-table-column prop="currentC" label="电流" align="center" />
        <el-table-column prop="voltageC" label="电压" align="center" />
        <el-table-column prop="totalActivePower" label="有功总电量" align="center" />
        <el-table-column prop="createTime" sortable label="创建时间" align="center" />
        <el-table-column prop="registered" label="是否注册" align="center">
          <template slot-scope="scope">
            {{scope.row.registered}}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="开/合闸" align="center">
          <template slot-scope="scope">
            {{scope.row.status}}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" sortable label="更新时间" align="center" />
        <el-table-column v-if="checkPer(['admin','meter:edit','meter:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMeter from '@/api/meter/meter.js'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { meterId: null, createTime: null, currentA: null, currentB: null, currentC: null, lat: null, lon: null, meterNum: null, registered: null, status: null, totalActivePower: null, updateTime: null, voltageA: null, voltageB: null, voltageC: null }
export default {
  name: 'Meter',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '电表数据', url: 'api/meter', idField: 'meterId', sort: 'meterId,desc', crudMethod: { ...crudMeter }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'meter:add'],
        edit: ['admin', 'meter:edit'],
        del: ['admin', 'meter:del']
      },
      rules: {
        meterNum: [
          { required: true, message: '表号不能为空', trigger: 'blur' }
        ],
        registered: [
          { required: true, message: '请选择注册状态', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'meterNum', display_name: '表号' },
        { key: 'registered', display_name: '是否注册' },
        { key: 'status', display_name: '开/合闸' }
      ],
      registeredTypeOptions: [
        { key: 'true', display_name: '已注册' },
        { key: 'false', display_name: '未注册' }
      ],
      statusTypeOptions: [
        { key: 'true', display_name: '开闸' },
        { key: 'false', display_name: '合闸' }
      ],
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
